<?php
/**
 * @copyright Copyright (c) 2021 勾股工作室
 * @license https://opensource.org/licenses/GPL-3.0
 * @link https://www.gougucms.com
 */

declare (strict_types=1);

namespace app\bitjob\controller;

use app\base\BaseController;
use app\bitjob\model\Bitjob as BitjobList;
use app\bitjob\model\Bitjob;
use app\License\validate\LicenseCheck;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;

class Index extends BaseController
{
    public function index()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $where = array();
            $whereOr = array();
            if (!empty($param['keywords'])) {
                $where[] = ['a.salesman_name|a.salesman_mobile|a.id_card', 'like', '%' . $param['keywords'] . '%'];
            }
            if (!empty($param['cert_major_type'])) {
                $where[] = ['a.cert_major_type', '=', $param['cert_major_type']];
            }
            if (!empty($param['p_type'])) {
                $where[] = ['a.p_type', '=', $param['p_type']];
            }

            if (!empty($param['cert_valid_date']) && !empty($param['cert_valid_date1'])) {
                $where[] = ['a.cert_valid_date', '>=', $param['cert_valid_date']];
                $where[] = ['a.cert_valid_date', '<=', $param['cert_valid_date1']];
            }
            $security_type = $param['security_type'] ?? '';
            if ($security_type != '') {
                $where[] = ['a.security_type', '=', $param['security_type']];
            }
            if ($param['index_from'] == 'self') {
                if ($this->uid != 1 && $this->uid != 3) {
                    $where[] = ['a.admin_id', '=', $this->uid];
                }
            }
            $where[] = ['a.delete_time', '=', 0];


            $model = new Bitjob();
            $list = $model->get_list($param, $where, $whereOr);
            return table_assign(0, '', $list);
        } else {
            View::assign('index_from', 'all');
            return view();
        }
    }


    public function findex()
    {
        View::assign('index_from', 'self');
        return view('index');
    }

    //添加&&编辑
    public function add()
    {
        $param = get_params();

        unset($param['file']);
        if (empty($param['reg_date'])) {
            unset($param['reg_date']);
        }
        if (empty($param['cert_valid_date'])) {
            unset($param['cert_valid_date']);
        }
        if (empty($param['abc_valid_date'])) {
            unset($param['abc_valid_date']);
        }

        if (request()->isAjax()) {

            if (empty($param['cert_major_type'])) {
                return to_assign(1, '请选择证书专业');
            }
            if (empty($param['p_type'])) {
                return to_assign(1, '请选择人员类型');
            }
            if (!empty($param['id']) && $param['id'] > 0) {
                unset($param['username']);
                unset($param['pwd']);


                $param['abc_type'] = implode(',', $param['abc_type']);
                $param['update_time'] = time();
                $old = Db::name('bitjob')->where(['id' => $param['id']])->find();
                $model = new Bitjob();
                if (!empty($model->where('id_card', $param['id_card'])->where('id', '<>', $param['id'])->where('delete_time', 0)->find())) {
                    return to_assign(1, '该人员身份证号以重复，请勿重复录入');
                }
                $res = $model->field(true)->update($param);
                if ($res) {
                    add_log('edit', $param['id'], $param);
                    to_log($this->uid, $param, $old);
                    return to_assign();
                } else {
                    return to_assign(1, '操作失败');
                }
            } else {


                $param['create_time'] = time();
                $param['admin_id'] = $this->uid;
                $param['abc_type'] = implode(',', $param['abc_type']);
                $model = new Bitjob();
                if (!empty($model->where('id_card', $param['id_card'])->where('delete_time', 0)->find())) {
                    return to_assign(1, '该人员身份证号以重复，请勿重复录入');
                }
                $aid = $model->field(true)->insertGetId($param);
                if ($aid) {
                    return to_assign();

                } else {
                    return to_assign(1, '操作失败');
                }
            }
        } else {
            $id = isset($param['id']) ? $param['id'] : 0;
            View::assign('id', $id);
            if ($id > 0) {
                $model = new Bitjob();
                $detail = $model->detail($id);
                $detail->abc_type = explode(',', $detail->abc_type);
                View::assign('detail', $detail);
            }
            return view();
        }
    }

    //删除
    public function delete()
    {
        if (request()->isDelete()) {
            $id = get_params("id");
            $data['id'] = $id;
            $data['delete_time'] = time();
            if (Db::name('bitjob')->update($data) !== false) {
                add_log('delete', $id);

                return to_assign(0, "删除成功");
            } else {
                return to_assign(1, "删除失败");
            }
        } else {
            return to_assign(1, "错误的请求");
        }
    }


}
